*****************************************************************
*****************************************************************
*****                                                       *****
*****       Mona Morgan-Collins (Durham University, UK)     *****
*****       Grace Natusch (The Civil Service, UK)           *****
*****       Contact: mona.morgan-collins@durham.ac.uk       *****
*****                                                       *****
*****       At the Intersection of Gender and Class:        *****
***** How Were Newly Enfranchised Women Mobilized in Sweden?*****
*****                                                       *****
*****           Comparative Political Sudies                *****
*****                                                       *****
*****           Building replication data sets              *****
*****                                                       *****
*****************************************************************
*****************************************************************

*Stata version 15.1
cd "./swe_repl" /*set your working directory*/ 

*install the following packages
ssc install coefplot, replace
ssc install spineplot, replace
ssc install egenmore, replace
ssc install binscatter, replace
ssc install mcenter, replace

*set matsize for Stata/MP and State/SE
set matsize 11000 , permanently 

*This file builds the following data sets:
	*sod2134 replicates key analyses in the paper and the appendix 
	*census1928 replicates analyses using census 1928 (Figures 5)
	*lab replicates analyses using labour statistics (Figure A16)
	
*All commands need to be run consecutively. 
*The analyses in the appendix needing to follow analyses in the paper. 
	
	
	
*****************************************************
*****************************************************
**         CREATING SODERTALJE 2134                **
*****************************************************
*****************************************************

import excel "./data_raw.xlsx", sheet("sod2134") firstrow case(lower) clear

destring (id year female ybirth right_munic voted_munic right_county voted_county), replace

gen age = .                                       //generating age
replace age = 1921-ybirth if year==1921
replace age = 1934-ybirth if year==1934
gen age2=age^2
mcenter age                                       //centering age
gen C_age2 = C_age*C_age

gen property = ground + " " + block               //generating property
egen property_id = group(property neighb year)    //generating id for each property by year
egen family_id = group(lastname property_id)	  //generating id for each family by year

gen wife = 0 if female==1                         //married women identified whose occupation is listed in registers as 'wife', indicating both employed and unemployed women.
replace wife = 1 if occup_eng=="Wife"

xtset family_id id                                //married men identified as those who live with their wives and are listed first in the registers
gen husband=0 if female==0
replace husband=1 if F.wife==1  & wife==.

gen married = 0                                   //generating binary indicator of married women and men
replace married=1 if wife==1
replace married=1 if husband==1	 
	 
egen wifemulti= total(wife), by(family_id)	      //identifying families with two or more wifes [robustness]

gen voted_mf = .                                                      //generating binary indicator for voting, by election and sex (for Figure 2)
replace  voted_mf = 0 if female==1 & right_munic==1
replace  voted_mf = 1 if female==1 & voted_munic==1 & right_munic==1
gen voted_mm = .
replace  voted_mm = 0 if female==0 & right_munic==1
replace  voted_mm = 1 if female==0 & voted_munic==1 & right_munic==1 
gen voted_cf = .
replace  voted_cf = 0 if female==1 & right_munic==1 
replace  voted_cf = 1 if female==1 & voted_county==1 & right_munic==1 
gen voted_cm = .
replace  voted_cm = 0 if female==0 & right_munic==1 
replace  voted_cm = 1 if female==0 & voted_county==1 & right_munic==1 
 
generate n1 = neigh=="Alhagen"                                         //binary indicators for each neighborhood [robustness]
generate n2 = neigh=="Atlas" 
generate n3 = neigh=="Brunnsäng" 
generate n4 = neigh=="Lägenh.u.stadsplan"
generate n5 = neigh=="Rosenborg"
generate n6 = neigh=="St Ragnhild"
generate n7 = neigh=="Tältet"
 
gen class_occup = .                               //generating class based on occupation listed in electoral register
replace class_occup = 3 if occup_cat=="I"         //working class
replace class_occup = 3 if occup_cat=="I_a"
replace class_occup = 3 if occup_cat=="I_b"
replace class_occup = 3 if occup_cat=="I_c"
replace class_occup = 3 if occup_cat=="II"
replace class_occup = 3 if occup_cat=="II_a"
replace class_occup = 3 if occup_cat=="II_b"
replace class_occup = 3 if occup_cat=="II_c"
replace class_occup = 3 if occup_cat=="II_d"
replace class_occup = 3 if occup_cat=="II_e"
replace class_occup = 3 if occup_cat=="II_f"
replace class_occup = 3 if occup_cat=="II_g"
replace class_occup = 3 if occup_cat=="II_h"
replace class_occup = 3 if occup_cat=="II_i"
replace class_occup = 3 if occup_cat=="III"
replace class_occup = 3 if occup_cat=="IV"
replace class_occup = 3 if occup_cat=="V"
replace class_occup = 2 if occup_cat=="VI_a"       //middle class
replace class_occup = 2 if occup_cat=="VI_b"
replace class_occup = 2 if occup_cat=="VI_c"
replace class_occup = 2 if occup_cat=="VII"
replace class_occup = 2 if occup_cat=="VIII"
replace class_occup = 2 if occup_cat=="IX"
replace class_occup = 1 if occup_cat=="X"          //upper class
replace class_occup = 0 if occup_cat=="0"          //dependent

xtset family_id id                                  
gen class_wife = class_occup                                          //generating class for `wifes' based on their husband's class
replace class_wife = L.class_occup if occup_eng=="Wife"  & L.wife==.  //class_wife has fewer observations, as `wifes' whose husband's occupation is missing are also now coded as missing
gen classw_rec = class_wife                                           //combining upper and middle class
replace classw_rec = 1 if class_wife==2

gen worker = class_wife==3                                            //generating binary indicator for working class
gen upmid = class_wife<3 & class_wife>0                               //generating binary indicator for upper & middle class
gen mid = class_wife==2                                               //generating binary indicator for middle class
gen depend = class_wife==0                                            //generating binary indicator for dependents
gen widow = occup_eng=="Widow"                                        //generating binary indicator for widows

gen electp = class_wife<=3 & class_wife>0                             //this set of commands generates number of neighbors in a property
egen telectp_all = total(electp), by(property_id year)                    
egen telectp_fam = total(electp), by(property_id year family_id)                    
gen telectp = telectp_all-telectp_fam

egen tworkp_all = total(worker), by(property_id year)                  //this set of commands generates the number of neighbors classified as working class/upper & middle class in a property
egen tworkp_fam = total(worker), by(property_id year family_id)    
gen tworkp = tworkp_all-tworkp_fam
egen tupmidp_all = total(upmid), by(property_id year)                  
egen tupmidp_fam = total(upmid), by(property_id year family_id)    
gen tupmidp = tupmidp_all-tupmidp_fam
egen tmidp_all = total(mid), by(property_id year)                  
egen tmidp_fam = total(mid), by(property_id year family_id)    
gen tmidp = tmidp_all-tmidp_fam

gen tworkp_pc =(tworkp/telectp)*100                                    //this set of commands generates the proportion of working/upper and middle class neighbors in a property among those with known class
gen tupmidp_pc =(tupmidp/telectp)*100                              
gen tmidp_pc =(tmidp/telectp)*100 

gen workXtworkp_pc = worker*tworkp_pc                                  //interaction
gen upmidXtupmidp_pc = upmid*tupmidp_pc                               
gen midXtmidp_pc = mid*tmidp_pc                               

gen electp_r = 1                                                       //this set of commands generates number of neighbors in a property [robustness]
egen telectp_all_r = total(electp_r), by(property_id year)                    
egen telectp_fam_r = total(electp_r), by(property_id year family_id)                    
gen telectp_r = telectp_all_r-telectp_fam_r

gen tworkp_pc_r =(tworkp/telectp_r)*100                               //this set of commanda geneates the proportion of working/upper and middle class neighbors in a property [robustness]
gen tupmidp_pc_r =(tupmidp/telectp_r)*100                              

gen workXtworkp_pc_r = worker*tworkp_pc_r                              //interaction [robustness]
gen upmidXtupmidp_pc_r = upmid*tupmidp_pc_r                               

gen workerf = worker==1                                                //this set of commands generates the number of women workers/upper and middle class
replace workerf = 0 if female==0

egen tworkfp_all = total(workerf), by(property_id year)                //this set of commands generates the number of neighbors classified as working class women in a property
egen tworkfp_fam = total(workerf), by(property_id year family_id)    
gen tworkfp = tworkfp_all-tworkfp_fam
gen tworkfp_pc =(tworkfp/telectp)*100                                  //this set of commands generates the proportion of working-class women neighbors in a property among those with known class

gen workXtworkfp_pc = worker*tworkfp_pc                                //interaction

egen tdepp_all = total(depend), by(property_id year)                   //no of electors classified as dependents in a property [robustness]
egen tdepp_fam = total(depend), by(property_id year family_id)    
gen tdepp = tdepp_all-tdepp_fam
gen tdepp_pc =(tdepp/telectp_all_r)*100                                //proportion of dependents among neighbors in a property
egen twidp_all = total(widow), by(property_id year)                    //no of electors classified as widow in a property
egen twidp_fam = total(widow), by(property_id year family_id)    
gen twidp = twidp_all-twidp_fam
gen twidp_pc =(twidp/telectp_all_r)*100                                //proportion of widows among neighbors in a property
  
gen tot = 1                                                            //this set of commands generates number of electors living in one's property [robustness fig A5]
egen nelecp = total(tot), by(property_id year) 

egen id_ind = group(neigh name ybirth)                                 //generating an id that indicates the same individual across the two data sets (based on neighborhood, year of birth and name) [robustness fig4]
duplicates tag id_ind, gen(long13)   

gen fworker= occup_eng=="Factory Worker"                               //identifying women factory workers [robustness Fig A9b]
replace fworker=1 if  occup_eng=="Former Factory Worker"

gen dworker= occup_eng=="Maid"                                         //identifying women domestic servants [robustness Fig A9a]
replace dworker=1 if  occup_eng=="Former Maid"
replace dworker=1 if  occup_eng=="Kitchen Maid"
replace dworker=1 if  occup_eng=="Kitchen Assistant"
replace dworker=1 if  occup_eng=="Housekeeper"
replace dworker=1 if  occup_eng=="Cook"
replace dworker=1 if  occup_eng=="Assistant Cook"
replace dworker=1 if  occup_eng=="Former Cook"
replace dworker=1 if  occup_eng=="Cleaner"
replace dworker=1 if  occup_eng=="Former Cleaner"

bys property_id: egen propsize=count(property_id)                       //generating the size (number of electors) of the property

bys property_id: egen avefamid = mean(family_id)                        //generating average of family_ids in the same property
gen sinfamprop=  (avefamid == family_id)                                //generating binary variable that indicates a single family property

gen votedm_rec = voted_munic if right_munic==1                          //prep for Figure A3
recode votedm_rec (0 =1) (1= 0)
gen votedc_rec = voted_county if right_county==1 
recode votedc_rec (0 =1) (1= 0)
gen groupstr = "W W" if worker==1 & female==1
replace groupstr = "W M" if worker==1 & female==0
replace groupstr = "UM W" if upmid==1 & female==1
replace groupstr = "UM M" if upmid==1 & female==0
replace groupstr = "D W" if depend==1 & female==1
replace groupstr = "D M" if depend==1 & female==0
encode groupstr, gen(group)

save "./sod2134.dta", replace

*adding soc dem membership
import excel "./data_raw.xlsx", sheet("socmemb") firstrow case(lower) clear
save "./socmemb.dta", replace

use "./sod2134.dta"
merge 1:1 id using "./socmemb.dta"                                        //merging with sod2134

gen member34 = 0
replace member34=1 if _merge==3 & year==1934 
bys property_id: egen member34p = max(member34) 
drop _merge

save "./sod2134.dta", replace


*****************************************************
*****************************************************
**         CREATING CENSUS 1928 DATA               **
*****************************************************
*****************************************************

import excel "./data_raw.xlsx", sheet("census1928") firstrow case(lower) clear

*data preparation
gen gg_u = round((turnf_u - turnm_u), 0.1)        //generating turnout gap
gen gg_w = round((turnf_w - turnm_w), 0.1) 

gen work_pc_num = _n
labmask work_pc_num, val(work_pc)

save "./census1928.dta"


*****************************************************
*****************************************************
**            CREATING LABOUR DATA                **
*****************************************************
*****************************************************

import excel "./data_raw.xlsx", sheet("lab") firstrow case(lower) clear

egen lab_lbf = rank(female_labour) 
labmask lab_lbf, val(country)

egen lab_ind = rank(ind_workers) 
labmask lab_ind, val(country)

save "./lab.dta"








